package com.file.system.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.system.WebDepartment;
import com.file.system.entity.system.WebDepartmentUser;
import com.file.system.service.system.IWebDepartmentService;
import com.file.system.service.system.IWebDepartmentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isNotEmpty;


/**
 * 部门
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/web/department")
public class WebDepartmentController {

    @Autowired
    private IWebDepartmentService service;
    @Autowired
    private IWebDepartmentUserService iWebDepartmentUserService;


    /**
     * 查询部门
     *
     * @param webDepartment 部门信息
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/get")
    public CommonResult<List<WebDepartment>> select(@RequestBody @Validated WebDepartment webDepartment) {
        QueryWrapper<WebDepartment> query = new QueryWrapper<>();
        //排序
        query.orderByAsc("a.sort");
        //判断是否删除
        query.eq("a.is_del", 0);
        //判断是否有父节点
        query.eq(ObjectUtil.isNotNull(webDepartment.getPid()), "pid", webDepartment.getPid());
        //判断名称是否为空
        query.like(isNotEmpty(webDepartment.getName()), "name", webDepartment.getName());
        //判断字典类型是否为空
        query.like(isNotEmpty(webDepartment.getDictType()), "dict_type", webDepartment.getDictType());

        //查询集合
        List<WebDepartment> list = service.list(query);
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过ids查询部门集合
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listByIds")
    public CommonResult<List<WebDepartment>> listByIds(@RequestBody List<Long> ids) {
        //根据ids查询部门信息
        List<WebDepartment> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
     * 查询所有非删除状态部门
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listAll")
    public CommonResult<List<WebDepartment>> listAll() {
        //查询集合
        List<WebDepartment> list = service.listAll();
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过id查询部门
     *
     * @param id 部门id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id}")
    public CommonResult<WebDepartment> getById(@PathVariable
                                               @Min(value = 1, message = "id长度最小1")
                                               Long id) {
        //查询集合
        WebDepartment webDepartment = service.getById(id);
        return CommonResult.ok().setResult(webDepartment);
    }


    /**
     * 通过id删除部门
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/remove/{id}")
    public CommonResult removeById(@PathVariable
                                   @Min(value = 1, message = "id长度最小1")
                                   Long id) {
        if (id == 1000000000000000000L) {
            return CommonResult.fail().setText("根节点禁止删除");
        }
        //判断该部门下是否有用户
        int count = iWebDepartmentUserService.count(new QueryWrapper<WebDepartmentUser>()
                .eq("a.fk_department_id", id)
        );
        if (count > 0) return CommonResult.fail().setText("该部门下还有用户");
        //删除
        boolean state = service.removeById(id);
        //返回
        return CommonResult.check(state);
    }


    /**
     * 通过id编辑部门
     *
     * @param webDepartment 部门
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/updateById")
    public CommonResult<WebDepartment> updateById(@RequestBody @Validated(WebDepartment.update.class) WebDepartment webDepartment) {
        //编辑修改
        boolean state = service.updateById(webDepartment);
        return CommonResult.check(state).setResult(webDepartment);
    }


    /**
     * 添加部门
     *
     * @param webDepartment 部门
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save")
    public CommonResult<WebDepartment> save(@RequestBody @Validated(WebDepartment.add.class) WebDepartment webDepartment) {
        //添加部门
        boolean state = service.save(webDepartment);
        return CommonResult.check(state).setResult(webDepartment);
    }

    /**
     * 部门添加账号
     *
     * @param webDepartmentUser 部门关联账号
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save/user")
    public CommonResult saveUser(@RequestBody @Validated(WebDepartmentUser.add.class) WebDepartmentUser webDepartmentUser) {
        QueryWrapper<WebDepartmentUser> query = new QueryWrapper<>();
        query.eq("fk_department_id", webDepartmentUser.getFkDepartmentId())
                .eq("fk_user_id", webDepartmentUser.getFkUserId());

        int count = iWebDepartmentUserService.count(query);
        boolean flag;
        if (count > 0) {
            // 更新
            flag = iWebDepartmentUserService.remove(webDepartmentUser);
        } else {
            // 插入
            flag = iWebDepartmentUserService.save(webDepartmentUser);
        }
        return CommonResult.check(flag);
    }
}

